<?php
namespace Home\Controller;
use Think\Controller;
use Think\AjaxPage;
use Think\Page;
class CartController extends Controller {
	public function _initialize() {
		$token = I('token')?I('token'):'';
		if(!$token){
			exit(json_encode(array('code'=>100,'msg'=>'验证失败','jn_msg'=>'確認に失敗しました')));
		}else{
			$u = M('users')->where(array('token'=>$token))->field('id,status')->find();	
			if(!$u['id']){
				exit(json_encode(array('code'=>400,'number'=>rand(1,99),'msg'=>'请先登录','jn_msg'=>'最初にログインしてください')));						
			}else if(!$u['status']){
				exit(json_encode(array('code'=>600,'number'=>rand(1,99),'cn'=>'您的账号已被冻结，请联系管理员。','jn'=>'あなたのアカウントは凍結されました。管理者に連絡してください。')));	
			}else{
				$this->user_id = $u['id'];
			}
		}
	}
    public function add_cart(){
		$user_id = $this->user_id;
		$goods_id = I('goods_id')?I('goods_id'):'';
		$goods_mod = M('goods');
		$goods = $goods_mod->where(array('id'=>$goods_id))->field('price,inventory,status')->find();
		if($goods['inventory']>0){
			if($goods['status']==1){
				$data['user_id'] = $user_id;
				$data['goods_id'] = $goods_id;
				$data['goods_number'] = 1;
				$data['price'] = $goods['price'];
				$data['add_time'] = time();
				$result = M('cart')->add($data);
				exit(json_encode(array('code'=>200,'data'=>$result)));
			}else{
				exit(json_encode(array('code'=>100,'msg'=>'该商品已下架','jn_msg'=>'アイテムが削除されました')));
			}
		}else{
			exit(json_encode(array('code'=>100,'msg'=>'库存不足','jn_msg'=>'在庫不足')));
		}
	}
	public function change_cart(){
		$user_id = $this->user_id;
		$cart_id = I('cart_id')?I('cart_id'):'';
		$type_id = I('type_id')?I('type_id'):'';
		$goods_mod = M('goods');
		$cart = M('cart')->where(array('id'=>$cart_id))->find();
		$goods = $goods_mod->where(array('id'=>$cart['goods_id']))->field('price,inventory,status')->find();
		if($type_id==1){
			$goods_number = $cart['goods_number']-1;
		}else{
			$goods_number = $cart['goods_number']+1;
		}
		if($goods['inventory']>=$goods_number){			
			if($goods['status']==1){
				$data['user_id'] = $user_id;
				$data['goods_id'] = $cart['goods_id'];
				$data['goods_number'] = $goods_number;
				$data['price'] = $goods['price'];
				$data['add_time'] = time();
				$result = M('cart')->where(array('id'=>$cart_id))->save($data);
				exit(json_encode(array('code'=>200,'data'=>$result)));
			}else{
				exit(json_encode(array('code'=>100,'msg'=>'该商品已下架','jn_msg'=>'アイテムが削除されました')));
			}
		}else{
			exit(json_encode(array('code'=>100,'msg'=>'库存不足','jn_msg'=>'在庫不足')));
		}
	}
	public function cart_info(){
		$user_id = $this->user_id;
		$address_id = I('address_id')?I('address_id'):'';
		$where['user_id'] = $user_id;
		$address_id && $where['address_id'] = $address_id;
		$address = M('users_addr')->where($where)->order('is_default desc')->find();	
		if($address['address_id']){
			exit(json_encode(array('code'=>200,'address'=>$address)));	
		}else{
			exit(json_encode(array('code'=>100)));	
		}
	}
	public function cart_list(){
		$user_id = $this->user_id;
		$cart_id = I('cart_id')?I('cart_id'):'';
		if($cart_id){
			$mod = M('cart');
			$goods = $mod->alias('as a')->field("a.goods_number,a.price,b.title,CONCAT('".C('WEB_URL')."',b.thumb) as thumb")->join('LEFT JOIN '.C('DB_PREFIX').'goods as b on a.goods_id=b.id')->where(array('a.id'=>$cart_id))->select();
			exit(json_encode(array('code'=>200,'data'=>$goods,'price'=>$goods[0]['goods_number']*$goods[0]['price'])));
		}else{
			exit(json_encode(array('code'=>100,'msg'=>'参数错误','jn_msg'=>'パラメータエラー')));
		}
	}
	
	public function cart_order(){
		$user_id = $this->user_id;
		$cart_id = I('cart_id')?I('cart_id'):'';
		$address_id = I('address_id')?I('address_id'):'';
		if($cart_id && $address_id){
			$cart = M('cart')->where(array('id'=>$cart_id))->find();
			$goods = M('goods')->where(array('id'=>$cart['goods_id']))->field('price,inventory,store_id,status')->find();
			if($goods['status']<1){
				exit(json_encode(array('code'=>100,'msg'=>'该商品已下架','jn_msg'=>'アイテムが削除されました')));
			}
			if($goods['inventory']<$cart['goods_number']){	
				exit(json_encode(array('code'=>100,'msg'=>'库存不足','jn_msg'=>'在庫不足')));
			}
			$address = M('users_addr')->where(array('address_id'=>$address_id))->find();
			
			$data['user_id'] = $user_id;
			$data['goods_id'] = $cart['goods_id'];
			$data['store_id'] = $goods['store_id'];
			$data['order_sn'] = date('YmdHis').rand(1000,9999);
			$data['goods_number'] = $cart['goods_number'];
			$data['order_status'] = 0;
			$data['price'] = $cart['goods_number']*$cart['price'];
			$data['consignee'] = $address['consignee'];
			$data['mobile'] = $address['mobile'];
			$data['address'] = $address['addr'].$address['address'];
			$data['shipping_name'] = I('shipping_name');
			$data['pay_name'] = I('pay_name');
			$data['message'] = I('message');
			$data['add_time'] = time();
			
			$result = M('order')->add($data);
			if($result){
				$log['order_id'] = $result;
				$log['resume'] = '确认订单';
				$log['add_time'] = time();
				M('order_log')->add($log);
				
				M('goods')->where(array('id'=>$cart['goods_id']))->save(array('inventory'=>$goods['inventory']-$cart['goods_number']));
				exit(json_encode(array('code'=>200,'data'=>$result)));
			}else{
				exit(json_encode(array('code'=>100,'msg'=>'参数错误','jn_msg'=>'パラメータエラー')));
			}
		}else{
			exit(json_encode(array('code'=>100,'msg'=>'参数错误','jn_msg'=>'パラメータエラー')));
		}
	}
}